package com.example.intentservicedemo;

import android.app.IntentService;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private Button btn_startService;
    private Button btn_stopService;
    private Button btn_bindService;
    private Button btn_unbindService;
    private Intent intent1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn_startService = (Button) findViewById(R.id.btn_startService);
        btn_stopService = (Button) findViewById(R.id.btn_stopService);
        btn_bindService = (Button) findViewById(R.id.btn_bindService);
        btn_unbindService = (Button) findViewById(R.id.btn_unbindService);
        btn_startService.setOnClickListener(this);
        btn_bindService.setOnClickListener(this);
        btn_stopService.setOnClickListener(this);
        btn_unbindService.setOnClickListener(this);
        //将数据发送到service中进行处理(好像做aidl很合适)
        Intent intent = new Intent(MainActivity.this,IntentService2Me.class);
        intent.putExtra("mhysa","mhysa发来的消息");
        startService(intent);
        intent1 = new Intent(MainActivity.this,MyService.class);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_startService:
                startService(intent1);
                break;
            case R.id.btn_stopService:
                stopService(intent1);
                break;
            case R.id.btn_bindService:
                bindService(intent1,serviceConnection, Context.BIND_AUTO_CREATE);
                break;
            case R.id.btn_unbindService:
                unbindService(serviceConnection);
                break;
        }
    }

    ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.i("mhysa","连接了connection");
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };
}
